package org.xtframe.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.xtframe.common.ActiveUserListener;
import org.xtframe.entity.ServerData;
import org.xtframe.entity.UserData;
import org.xtframe.service.AjaxDataHandler;
import org.xtframe.service.AjaxDataHandlerException;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.ContextUtil;

@Service
@Scope("prototype")
public class ServerDataListDataHandler implements AjaxDataHandler {

	private final Log logger = LogFactory.getLog(getClass());

	public Map<String, Object> execute(String strData) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			UserData userData = ContextUtil.getCurrentUserData();
			ServerData serverData = ContextUtil.getServerData();
			
			JSONObject objJson = JSONObject.fromObject(strData);
			JSONArray arrNameValue = objJson.optJSONArray("arrNameValue");
			if(arrNameValue == null) throw new AjaxDataHandlerException("批量获取服务器数据的名称为空");
			
			ArrayList<Object> dataList = new ArrayList<Object>();
			for(int i=0; i<arrNameValue.size(); i++) {
				ArrayList<String> tempList = CommonUtil.JSONArrayToArrayList_String(arrNameValue.optJSONArray(i));
				if(tempList.size() > 0){
					String name = tempList.get(0);
					if(name.equals("uuid")){
						dataList.add(CommonUtil.getUUID());
						continue;
					}
					else if(name.equals("optrole")){
						boolean flag = false;
						ArrayList<String> roleList = userData.getRoleList();
						for(String roleid : roleList){
							if(roleid.equals("admin")){
								flag = true;
								break;
							}
						}
						
						if(!(flag && (userData.getUser().getOrganId() == 1) && (userData.getUser().getJobId() == 1))){
							ArrayList<Map<String, String>> optRoleList = userData.getOptRoleList();
							if(optRoleList == null) throw new AjaxDataHandlerException(2);
							dataList.add(optRoleList);
						}
						else{
							ArrayList<Map<String, String>> optRoleList = serverData.getOptRole();
							if(optRoleList == null) throw new AjaxDataHandlerException(2);
							dataList.add(optRoleList);
						}
						continue;
					}
					else if(name.equals("regid")){
						if(tempList.size() <= 1) throw new AjaxDataHandlerException(2);
						
						ArrayList<Long> regList = new ArrayList<Long>();
						for(int j=1; j<tempList.size(); j ++){
							String regName = tempList.get(j);
							regList.add(CommonUtil.getRegID(regName));
						}
						dataList.add(regList);
						continue;
					}
					else if(name.equals("logic")){
						if(tempList.size() <= 1) throw new AjaxDataHandlerException(2);
						
						ArrayList<Boolean> bLogicList = new ArrayList<Boolean>();
						for(int j=1; j<tempList.size(); j ++){
							String logicId = tempList.get(j);
							bLogicList.add(userData.isInLogic(logicId));
						}
						dataList.add(bLogicList);
						continue;
					}
					else if(name.equals("curr_date")){
						dataList.add((new Date()).toString());
						continue;
					}
					else if(name.equals("curr_user_atorganid")){
						dataList.add(userData.getOrgan().getAtOrganId());
						continue;
					}
					else if(name.equals("curr_user_organid")){
						dataList.add(userData.getOrgan().getOrganId());
						continue;
					}
					else if(name.equals("dictionary")){
						if(tempList.size() <= 1) throw new AjaxDataHandlerException(2);
						
						ArrayList<ArrayList<Map<String, String>>> dicList = new ArrayList<ArrayList<Map<String, String>>>();
						for(int j=1; j<tempList.size(); j ++){
							String dictionaryId = tempList.get(j);
							dicList.add(serverData.getDictionaryDataList(dictionaryId));
						}
						dataList.add(dicList);
						continue;
					}
					else if(name.equals("organdata")){
						if(tempList.size() <= 1) throw new AjaxDataHandlerException(2);
						
						ArrayList<String> odList = new ArrayList<String>();
						for(int j=1; j<tempList.size(); j ++){
							String dataName = tempList.get(j);
							odList.add(userData.getOrganDataValue(dataName));
						}
						dataList.add(odList);
						continue;
					}
					else if(name.equals("systemdata")){
						if(tempList.size() <= 1) throw new AjaxDataHandlerException(2);
						
						ArrayList<String> sdList = new ArrayList<String>();
						for(int j=1; j<tempList.size(); j ++){
							String dataName = tempList.get(j);
							sdList.add(serverData.getSystemDataValue(dataName));
						}
						dataList.add(sdList);
						continue;
					}
					else if(name.equals("useronlinecount")){
						dataList.add(ActiveUserListener.getUserOnlineCount());
						continue;
					}
					else if(name.equals("useronlinecode")){
						dataList.add(ActiveUserListener.getUserOnlineCode());
						continue;
					}else{
						throw new AjaxDataHandlerException(2);
					}
				} else {
					throw new AjaxDataHandlerException(2);
				}
			}
			map.put(STATUS, true);
			map.put(LIST, dataList);
		} catch(AjaxDataHandlerException ex){
			if(ex.getFlag() != 2) logger.warn(ex.toString());
			if(ex.getFlag() != 1){
				map.clear();
				map.put(STATUS, false);
			}
		} catch (Exception e) {
			logger.error(e.toString());
			map.clear();
			map.put(STATUS, false);
		}
		
		return map;
	}

}
